//
//  nxzViewController.m
//  projectAlcohol
//
//  Created by karan kadakia on 11/14/13.
//  Copyright (c) 2013 home. All rights reserved.
//

#import "nxzViewController.h"

@interface nxzViewController ()

@end

@implementation nxzViewController
@synthesize txtName,postURL,postArgX,postArgY,postArgZ,outputPost;
@synthesize responseData = _responseData;

//@synthesize value1TextField,value2TextField;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    postURL.delegate = self;
    txtName.delegate = self;
    postArgZ.delegate = self;
    postArgY.delegate = self;
    postArgX.delegate = self;
    self.responseData = [NSMutableData data];
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField) {
        [textField resignFirstResponder];
    }
    return NO;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [txtName resignFirstResponder];
    [postArgX resignFirstResponder];
    [postArgY resignFirstResponder];
    [postArgZ resignFirstResponder];
    [postURL resignFirstResponder];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)getDown{
    
    nxzCustomAlertViewController *alertView = [[nxzCustomAlertViewController alloc]init];
    [alertView setContainerView:[self createGET]];
    
    [alertView setButtonTitles:[NSMutableArray arrayWithObjects:@"Ok", nil]];
    //[alertView setDelegate:self];
    [alertView setOnButtonTouchUpInside:^(nxzCustomAlertViewController *alertView, int buttonIndex) {
        NSLog(@"Block: Button at position %d is clicked on alertView %d.", buttonIndex, [alertView tag]);
        [alertView close];
    }];
    
    [txtName resignFirstResponder];
    [postURL resignFirstResponder];
    [alertView setUseMotionEffects:true];
    [alertView show];
}

- (void)customAlertViewdialogButtonTouchUpInside: (nxzCustomAlertViewController *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex {
    NSLog(@"Delegate: Button at position %d is clicked on alertView %d.", buttonIndex, [alertView tag]);
    [alertView close];
}

-(UIView *)createGET {
    
    UIView *View = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 300)];
    
    UITextView *outputGET = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 260, 250)];
    [outputGET setDelegate:self];
    outputGET.backgroundColor = [UIColor yellowColor];
    NSString *strURL = [NSString stringWithFormat:@"http://localhost/project-alcohol/index.php/%@/%@",postURL.text,txtName.text];
    NSLog(@"%@",strURL);
    NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
    NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
    outputGET.text = strResult;
    
    [View addSubview:outputGET];
    return View;
    
}

-(IBAction)postDown:(id)sender {
   
    NSString *strURL = [NSString stringWithFormat:@"http://localhost/project-alcohol/index.php/%@",postURL.text];
    
//    NSString *bodyData = [NSString stringWithFormat:@"%@/%@/%@",postArgX.text,postArgY.text,postArgZ.text];
    NSString *bodyData = [NSString stringWithFormat:@"&username=%@&password=%@",postArgX.text,postArgY.text];
    
    NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strURL]];
    [postRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [postRequest setHTTPMethod:@"POST"];
    [postRequest setHTTPBody:[NSData dataWithBytes:[bodyData UTF8String] length:strlen([bodyData UTF8String])]];
    
    //calling line
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:postRequest delegate:self];
    
    NSLog(@"%@",connection);
    NSLog(@"%@",bodyData);
    
    if (!connection) {
        NSLog(@"Connection Failed");
    }
    
    [postURL resignFirstResponder];
    [postArgX resignFirstResponder];
    [postArgY resignFirstResponder];
    [postArgZ resignFirstResponder];
    
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"didReceiveData");
    [self.responseData appendData:data];
    outputPost.text = [[NSString alloc] initWithData:self.responseData encoding:NSASCIIStringEncoding];
}

@end
